# coding=utf-8
import pymongo
import developconf
import productconf
import os
from reedlive_scrapy.items import ChannelItem


class DbManager(object):
    """docstring for DbManager"""

    def __init__(self):
        super(DbManager, self).__init__()
        if os.environ["NODE_ENV"]=='develop':
            self.client = pymongo.MongoClient(developconf.MONGODB_URL)
            self.db = self.client[developconf.MONGODB_DB]
        else:
            self.client = pymongo.MongoClient(productconf.MONGODB_URL)
            self.db = self.client[productconf.MONGODB_DB]

    # 获取渠道列表
    def getItems(self):
        collection_name = 'rsschannels'
        collection = self.db[collection_name]
        channelList = set()
        for item in collection.find():
            channelModel = ChannelItem()
            channelModel['title'] = item["title"]
            channelModel['link'] = str(item["url"])
            channelModel['rsstype'] = self.getChannelType(item['rssType'])
            channelList.add(channelModel)
        return channelList

    # 获取渠道类型
    def getChannelType(self, channelId):
        collection_name = 'rsstypes'
        collection = self.db[collection_name]
        rsstype = collection.find_one({"_id": channelId})
        return rsstype['data']

    # 获取渠道id
    def getChannelId(self, url):
        collection_name = 'rsschannels'
        collection = self.db[collection_name]
        channel=collection.find_one({'url':url})
        return channel['_id']

    # 获取渠道地址
    def getChannelFromUrl(self, url):
        collection_name = 'rsschannels'
        collection = self.db[collection_name]
        channel=collection.find_one({'url':url})
        return channel

    # 获取item类型
    def getRssType(self, id):
        collection_name = 'rsstypes'
        collection = self.db[collection_name]
        rsstype=collection.find_one({'_id':id})
        return rsstype['data']

    # 更新去得到
    def updateChannelTime(self,channel):
        collection_name = 'rsschannels'
        collection = self.db[collection_name]
        if collection.find_one({'url':channel['url']}):
            collection.save(channel)

    def updateTuikuItem(self,mid,content,subTitle,textCount):
        collection_name = 'rssitems'
        collection=self.db[collection_name]
        col= collection.find_one({"_id":mid})
        if col:
            col['content']=content
            col['subTitle']=subTitle
            col['textCount']=textCount
            collection.save(col)


    def updateWeChatItem(self,id,content,subTitle,textCount,link):
        collection_name = 'rssitems'
        collection=self.db[collection_name]
        col= collection.find_one({"_id":id})
        if col:
            col['content']=content
            col['subTitle']=subTitle
            col['textCount']=textCount
            col['link']=link
            collection.save(col)


    #判断rssitem是否存在
    def isItemExist(self,link):
        collection_name = 'rssitems'
        collection=self.db[collection_name]
        if collection.find_one({"link":link}):
            return True
        else:
            return False

    #判断推酷item是否存
    def isIdExist(self,id):
        collection_name = 'rssitems'
        collection=self.db[collection_name]
        if collection.find_one({"mid":id}):
            return True
        else:
            return False

    def isWeChatIdExist(self, id):
        collection_name = 'rssitems'
        collection = self.db[collection_name]
        if collection.find_one({"wid": id}):
            return True
        else:
            return False




